package com.rabbiter.oes.controller;

import com.rabbiter.oes.entity.ApiResult;
import com.rabbiter.oes.entity.JudgeQuestion;
import com.rabbiter.oes.serviceimpl.JudgeQuestionServiceImpl;
import com.rabbiter.oes.util.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 判断题控制器类，处理判断题相关的HTTP请求
 * 包含新增、查询唯一ID、修改判断题等功能
 */
@RestController
public class JudgeQuestionController {

    @Autowired
    private JudgeQuestionServiceImpl judgeQuestionService;

    /**
     * 新增判断题接口
     * @param judgeQuestion 通过请求体接收的判断题实体对象，包含题目内容、答案等详细信息
     * @return ApiResult 包含操作状态码、消息和影响行数的响应对象
     *                  成功返回200状态码，失败返回400状态码
     */
    @PostMapping("/judgeQuestion")
    public ApiResult add(@RequestBody JudgeQuestion judgeQuestion) {
        // 调用服务层进行新增操作
        int res = judgeQuestionService.add(judgeQuestion);
        // 根据影响行数判断操作结果
        if (res != 0) {
            return ApiResultHandler.buildApiResult(200,"添加成功",res);
        }
        return ApiResultHandler.buildApiResult(400,"添加失败",res);
    }

    /**
     * 获取最新判断题ID接口
     * @return ApiResult 包含操作状态码、消息和最新判断题ID的响应对象
     *                  始终返回200状态码和查询结果
     */
    @GetMapping("/judgeQuestionId")
    public ApiResult findOnlyQuestionId() {
        // 获取最新创建的判断题ID
        JudgeQuestion res = judgeQuestionService.findOnlyQuestionId();
        return  ApiResultHandler.buildApiResult(200,"查询成功",res);
    }

    /**
     * 修改判断题接口
     * @param judgeQuestion 通过请求体接收的修改后判断题实体对象
     * @return ApiResult 包含操作状态码、消息和影响行数的响应对象
     *                  成功返回200状态码，失败返回400状态码
     */
    @PostMapping("/editJudgeQuestion")
    public ApiResult edit(@RequestBody JudgeQuestion judgeQuestion) {
        // 调用服务层进行修改操作
        int res = judgeQuestionService.edit(judgeQuestion);
        // 根据影响行数判断操作结果
        if (res != 0) {
            return ApiResultHandler.buildApiResult(200,"修改成功",res);
        }
        return ApiResultHandler.buildApiResult(400,"修改失败",res);
    }
}
